<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Todoes_Controller extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('xlsimport/php-excel-reader/excel_reader2');
        $this->load->helper('xlsimport/spreadsheetreader.php');
        $this->load->model('todo', '', true);
        $this->load->model('user', '', true);

        if (!$this->session->userdata('logged_in')) {
            redirect('login');
        }

        //$this->LoggedUser = $this->user->getLoggedUser();

        $this->load->library('authentication');
        $this->load->helper('t');
        $this->loggedUser = $this->authentication->getLoggedUser();
        $this->authentication->redirectIfNotBe(array('HR', 'SC'));
    }

    function index($limit = '0') {
//        $logged_in = $this->session->userdata('logged_in');
//        if ($logged_in['su'] != "1") {
//            exit('Permission denied');
//            return;
//        }

        $this->load->helper('form');

//        $viewData['category'] = $this->category->category_dropdown();
//        $viewData['difficult_level'] = $this->difficult_level->level_dropdown();
//        $viewData['result'] = $this->qbank_model->question_list($limit);
//        $viewData['title'] = "Question Bank";
//        $viewData['limit'] = $limit;

        $viewData['todoes'] = $this->todo->get_list($limit);


        $this->load->view(get_folder($this->loggedUser->title) . '/todoes_list', $viewData);
    }

    function add() {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');

        if ($this->input->post('Command') && $this->input->post('Command') == 'Add') {
            // Thực hiện form validation
            $this->form_validation->set_rules('title', 'Tên công việc', 'required');
            $this->form_validation->set_rules('access', 'Đối tượng có thể xem', 'required');
            if ($this->form_validation->run() == false) {
                
            } else {
                $this->load->model('evaluation');
                $e = $this->evaluation->get_last();

                $newRecord = array(
                    'title' => $this->input->post('title'),
                    'access' => implode(',', $this->input->post('access')),
                    'start_day' => $this->input->post('start_day'),
                    'end_day' => $this->input->post('end_day'),
                    'note' => $this->input->post('note'),
                    'eid' => $e['id'],
                );
                $viewData['message'] = $this->todo->insert($newRecord);
            }

            $viewData['error'] = false;
        }


        $this->load->view(get_folder($this->loggedUser->title) . '/todo_add', $viewData);
    }

    function edit($id) {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');


        if ($this->input->post('Command') && $this->input->post('Command') == 'Edit') {
            $this->form_validation->set_rules('title', 'Tên công việc', 'required');
            $this->form_validation->set_rules('access', 'Đối tượng có thể xem', 'required');
            if ($this->form_validation->run() == false) {
                
            } else {
//                $this->load->model('evaluation');
//            $e = $this->evaluation->get_last();

                $newRecord = array(
                    'id' => $id,
                    'title' => $this->input->post('title'),
                    'access' => implode(',', $this->input->post('access')),
                    'start_day' => $this->input->post('start_day'),
                    'end_day' => $this->input->post('end_day'),
                    'note' => $this->input->post('note'),
                        //'eid' => $e['id'], // khi add thì cần, edit phải giữ nguyên
                );

                $viewData['message'] = $this->todo->update($newRecord);
            }
        }

        $todo = $this->todo->get($id);
        $viewData['todo'] = $todo;
        $viewData['access'] = explode(',', $todo['access']);

        $this->load->view(get_folder($this->loggedUser->title) . '/todo_edit', $viewData);
    }

    function delete($id) {
//        $logged_in = $this->session->userdata('logged_in');
//        if ($logged_in['su'] != "1") {
//            exit('Permission denied');
//            return;
//        }
        $viewData['deleteResult'] = $this->todo->delete($id);

        redirect(get_folder($this->loggedUser->title) . '/home', 'refresh');
    }

}
?>













